package io.github.shang11925.sbvuewelearn.controller;

import io.github.shang11925.sbvuewelearn.dto.SectionsDTO;
import io.github.shang11925.sbvuewelearn.entity.Sections;
import io.github.shang11925.sbvuewelearn.response.SectionsResponse;
import io.github.shang11925.sbvuewelearn.service.SectionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin("http://localhost:5173/")
public class SectionsController {
    @Autowired
    SectionsService sectionsService;

    @GetMapping("/list-sections")
    public SectionsResponse list(){
        List<Sections> sections = sectionsService.findAll();
        SectionsResponse sectionsResponse = new SectionsResponse();
        sectionsResponse.setSections(sections);
        return sectionsResponse;
    }

    @GetMapping("/list-sections/{chapterId}")
    public SectionsResponse list(@PathVariable Long chapterId){
        List<Sections> sections = sectionsService.findByChapterId(chapterId);
        SectionsResponse sectionsResponse = new SectionsResponse();
        sectionsResponse.setSections(sections);
        return sectionsResponse;
    }

    @PostMapping("add-sections")
    public void add(@RequestBody SectionsDTO sectionsDTO){
        sectionsService.add(sectionsDTO);
    }

    @DeleteMapping("/delete-sections/{id}")
    public void delete(@PathVariable Long id){
        sectionsService.delete(id);
    }
}